﻿using Microsoft.AspNetCore.Mvc;
using SocketGateway.DTO;
using TK.TSocket;
using TouchSocket.Core;

namespace SocketGateway.Controllers
{
    /// <summary>
    /// socket页面
    /// </summary>
    [Route("socket")]
    public class SocketController : Controller
    {
        private readonly ITcpSocketServer _socketServer;

        /// <summary>
        /// ctor
        /// </summary>
        public SocketController(ITcpSocketServer socketServer)
        {
            _socketServer = socketServer;
        }

        /// <summary>
        /// 监听器页面
        /// </summary>
        [HttpGet("listener")]
        public IActionResult Listener()
        {
            return View();
        }

        /// <summary>
        /// 客户端页面
        /// </summary>
        [HttpGet("client")]
        public IActionResult SocketClient()
        {
            return View();
        }

        /// <summary>
        /// 黑白名单页面
        /// </summary>
        /// <returns></returns>
        [HttpGet("white_black")]
        public IActionResult WhiteBlack()
        {
            var tcpConfig = _socketServer.TcpService.Container.Resolve<TcpSocketServerConfig>();
            var data = new WhiteBlackDto();
            if (tcpConfig != null)
            {
                if (tcpConfig.IpBlackList?.Any() == true)
                    data.IpBlackList.AddRange(tcpConfig.IpBlackList);
                if (tcpConfig.IpWhiteList?.Any() == true)
                    data.IpWhiteList.AddRange(tcpConfig.IpWhiteList);
            }

            return View(data);
        }
    }
}
